package edu.whu.tao.util;

import java.io.UnsupportedEncodingException;
import java.security.*;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

/**
 * 
 * <P>AES加密解密</P>
 * @author 陶焕(13294175866)
 * @date 2016年4月17日 下午8:19:36
 */
public class AESUtils {

	private AESUtils() {
	}

	private static class AESUtilsFactory {
		private static final AESUtils instance = new AESUtils();
	}
	
	public static AESUtils getInstance() {
		return AESUtilsFactory.instance;
	}

	public String encrypt(String msg) {
		try {
			java.security.Key key = new SecretKeySpec(keybytes, "AES");
			Cipher in = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
			in.init(1, key, new IvParameterSpec(iv));
			byte enc[] = in.doFinal(msg.getBytes("UTF-8"));
			String str = new String(Hex.encode(enc));
			return str;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String decrypt(String msg) {
		try {
			java.security.Key key = new SecretKeySpec(keybytes, "AES");
			Cipher out = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
			out.init(2, key, new IvParameterSpec(iv));
			byte dec[] = out.doFinal(Hex.decode(msg.getBytes("UTF-8")));
			String str = new String(dec);
			return str;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	byte iv[] = { 56, 55, 54, 53, 52, 51, 50, 49, 56, 55, 54, 53, 52, 51, 50, 49 };
	byte keybytes[] = { 112, 47, 23, 127, 108, 58, 34, 17, 63, 68, 90, 102, 119, 26, 18, 28 };

	static {
		Security.addProvider(new BouncyCastleProvider());
	}
	
	
	/**
	 * 测试
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		AESUtils aes = AESUtils.getInstance();
		String str = "root";
		LogUtils.println("加密后", aes.encrypt(str));
		LogUtils.println("解密后", aes.decrypt(aes.encrypt(str)));
		str = "taohuan";
		LogUtils.println("加密后", aes.encrypt(str));
		LogUtils.println("解密后", aes.decrypt(aes.encrypt(str)));
	}
}
